﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary;
using System.Data.SqlClient;

namespace ClassLibrary
{
    public class GatewayUserContent : DataAccess<UserContent>
    {
        public override UserContent Create(UserContent objectToCreate)
        {
            CreateCon();

            System.Data.IDbCommand command = con.CreateCommand();

            command.CommandText = "INSERT INTO UserContent "
                                    + "(CPR, Description) VALUES " +
                                     "('" + objectToCreate.Cpr + "', '" + objectToCreate.Description + "')";

            int antalIndsat = command.ExecuteNonQuery();
            if (antalIndsat == 1)
            {
                command.CommandText = "SELECT @@identity AS lastinsertedkey";
                string slastkey = command.ExecuteScalar().ToString();
                int lastkey = int.Parse(slastkey);
                //7
                //objectToCreate.ID = lastkey;
                EndCon();
                return objectToCreate;
            }
            EndCon();
            return null;
        }

        public override UserContent Update(UserContent objectToUpdate)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(UserContent objectToDelete)
        {
            throw new NotImplementedException();
        }

        public override UserContent Load(int idOfObjectToLoad)
        {
            throw new NotImplementedException();
        }

        //public override List<UserContent> LoadAll()
        //{
            //CreateCon();
            //var ListOfUserContent = new List<UserContent>();
            
            //string sql = "SELECT CPR, DESCRIPTION FROM UserContent ORDER BY id ASC' ";

            //using (var command = new SqlCommand(sql)) {
            //    using (var reader = command.ExecuteReader()) {
            //        while (reader.Read())
            //        {
            //            var usercontent = new SqlUserContent();

            //            usercontent.Cpr = reader["Cpr"].ToString();
            //            usercontent.Description = reader["Description"].ToString();
            //            ListOfUserContent.Add(usercontent);

            //        }
            //    }
            //}
            //EndCon();
            //return ListOfUserContent;
        //}






        public override UserContent LoadAll()
        {
            CreateCon();
            var ListOfUserContent = new List<UserContent>();

            string sql = "SELECT CPR, DESCRIPTION FROM UserContent ORDER BY id ASC' ";

            using (var command = new SqlCommand(sql))
            {
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        ContentCollection userContentList = new ContentCollection();
                        userContentList.CreateUserContent(reader["Cpr"].ToString(), reader["Description"].ToString());
                    }
                }
            }
            EndCon();
            return null;
        }

    }


}



